package iplocation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by donar on 17/7/27.
 */
public class ANYTASK {
    static final String YOUR_COOKIE = "cookieid=0BC7F4AC1ABDBA3A6246F3CBD2831390; _ati=999220288374; Hm_lvt_6a46176ff83d6f29a3390a8ae6352d0a=1499658486; __utma=250501124.1029095747.1500362230.1500892265.1500948986.7; __utmz=250501124.1500362230.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=250501124.|1=versionnum=3=1^3=level=1=1; skin_rgb=74b9e1; safe_ip=192.168.50.26; safe_identify=%E9%97%BE%E9%93%8E; safe_sign=0768eddc73fb5956b14162ee1dd9b7ad; safe_time=1503452453413; safe_user=%E9%97%BE%E9%93%8E; JSESSIONID=8AA9F8FD10BA44E6A2E913D792239F41; super_memSessionId1=6d6b412a6e6e2ac1585e7f4c7f17fa5dfd84b3da617547944bee2d83d2596372b734548797a414bd31ba12af712701e26db1f860d5404ad0205caccc50cc158c; superuseragent1=4003a5039f0e8f7a5ba7cc0603669c089e9e77724e6c62093d768ecdbefa044b5e28939ced8cfddea6efeee3e7ac89b07db2083301dcb8d1ee8dcd062c830a8cb8bcb576a87cde656d1383b848ef44b2e15e247ae1f94701b63c8ce828cf0728774d3e2a8268f74b5def0d517e74d3f4e43da585df59ac74d6450cf87ea8c6ef; h_identify=7ad71e2130e0dc105eccc47003a1b5dc; identify=dd847f5238223974cf7dda830379071e";
    //static LinkedBlockingQueue<JSONObject> taskDomains = new LinkedBlockingQueue<JSONObject>();
    static CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
    public static void main(String[] args) throws IOException {
        String sql = "select * from shop where platform_type = 4";
        String data = getData(sql);
        JSONObject o = JSON.parseObject(data);
        JSONArray array = o.getJSONObject("data").getJSONArray("dataList");
        for (Object d : array) {
            JSONObject jsonObject = (JSONObject) d;
            //TODO 对单条数据进行处理
        }



    }


    public  static String getData(String sql) throws IOException {
        HttpPost post = new HttpPost("http://sql.superboss.cc/query/doQueryObjectList");
        post.setHeader("Referer","http://sql.superboss.cc/new_index.jsp");
        post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        post.setHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36");
        post.setHeader("Origin","http://sql.superboss.cc");
        post.setHeader("Cookie", YOUR_COOKIE);
        post.setHeader("X-Requested-With","XMLHttpRequest");
        post.setHeader("Accept","*/*");
        post.setHeader("Accept-Encoding","gzip, deflate");
        post.setHeader("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6,pt;q=0.4");
        post.setHeader("Host","sql.superboss.cc");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();

        nvps.add(new BasicNameValuePair("pageSize", "20000"));//每页6000条
        nvps.add(new BasicNameValuePair("pageNo", "1"));
        nvps.add(new BasicNameValuePair("dbId", "178"));
        nvps.add(new BasicNameValuePair("schemaName", "overseas1"));
        nvps.add(new BasicNameValuePair("url", "kuajing-1.mysql.rds.aliyuncs.com"));
        nvps.add(new BasicNameValuePair("sql", sql));
        post.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response = closeableHttpClient.execute(post);
        String data = EntityUtils.toString(response.getEntity());
        response.close();
        //System.out.println(data);
        return data;
    }

}
